package cac.fenjian.resource.workbinMaterialMaintain.service;


import cac.fenjian.resource.workbinMaterialMaintain.dao.workbinMaterialMaintainDao;
import com.cac.dme.adapter.rest.msg.QueryReqBean;
import com.cac.dme.adapter.rest.msg.QueryRespBean;
import com.cac.dme.adapter.rest.msg.ResponseMsg;
import com.cac.dme.adapter.utils.PageHelper;
import com.cac.dme.common.exception.service.DaoException;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;

@Service
public class workbinMaterialMaintainService {
    @Autowired
    private workbinMaterialMaintainDao workbinMaterialMaintaindao;

    @Transactional
    /*缓存料箱物料信息维护查询*/
    public QueryRespBean<HashMap> workbinMaterialMaintainSearch(QueryReqBean<HashMap> queryReqBean, String orderBy, String Keywords) {
        QueryRespBean<HashMap> queryRespBean = new QueryRespBean<>();
        try {
            PageHelper.startPage(queryReqBean.getPageParameter());
            //com.github.pagehelper.PageHelper.startPage(queryReqBean.getPageParameter().getPage(),queryReqBean.getPageParameter().getRows());
            HashMap searchParams = queryReqBean.getSearchParams();
            Page<HashMap> dataList = workbinMaterialMaintaindao.workbinMaterialMaintainSearch(searchParams, orderBy, Keywords);
            queryRespBean.setResult(dataList);
            return queryRespBean;
        } catch (Exception e) {
            throw new DaoException(e.getMessage(), e);
        }
    }

    @Transactional
    /*判断查询是否为空*/
    public List<HashMap> workbinMaterialMaintainSearch(HashMap searchParams, String orderBy, String Keywords) {
        try {
            List<HashMap> dataList = workbinMaterialMaintaindao.workbinMaterialMaintainSearch(searchParams, orderBy, Keywords);
            return dataList;
        } catch (Exception e) {
            throw new DaoException(e.getMessage(), e);
        }
    }

    @Transactional
    /*缓存料箱物料信息维护新增*/
    public int workbinMaterialMaintainInsert(HashMap map) {
        return workbinMaterialMaintaindao.workbinMaterialMaintainInsert(map);
    }


    public int checkItemExists(HashMap map){
        return workbinMaterialMaintaindao.queryWorkbinRuleItemExists(map);
    }

    @Transactional
    /*缓存料箱物料信息维护修改*/
    public int workbinMaterialMaintainUpdate(HashMap hashmap) {
        String id = (String) hashmap.get("msWorkbinRuleId");
        if (StringUtils.isEmpty(id)) {
            throw new DaoException("修改失败！传入的参数主键为null");
        }
        try {
            //记录日志
            HashMap map = workbinMaterialMaintaindao.workbinMaterialMaintainFindById(id);
            if (map == null) {
                throw new DaoException("修改失败！对象不存在");
            }
            //修改业务数据
            return workbinMaterialMaintaindao.workbinMaterialMaintainUpdate(hashmap);
        } catch (Exception e) {
            throw new DaoException(e.getMessage(), e);
        }
    }

    @Transactional
    /*查询已有缓存料箱编码*/
    public HashMap workbinFindByWorkbinCode(HashMap hashmap) {
        String id = (String) hashmap.get("workbinCode");
        return workbinMaterialMaintaindao.workbinFindByWorkbinCode(id);
    }

    @Transactional
    /*缓存料箱物料信息维护删除*/
    public int workbinMaterialMaintainDelete(HashMap hashmap) {
        String id = (String) hashmap.get("msWorkbinRuleId");
        if (StringUtils.isEmpty(id)) {
            throw new DaoException("删除失败！传入的参数主键为null");
        }
        try {
            //记录日志
            HashMap map = workbinMaterialMaintaindao.workbinMaterialMaintainFindById(id);
            if (map == null) {
                throw new DaoException("删除失败！对象不存在");
            }
            //删除业务数据
            int count = workbinMaterialMaintaindao.workbinMaterialMaintainDelete(id);
            return count;
        } catch (Exception e) {
            throw new DaoException(e.getMessage(), e);
        }
    }
}
